package tech.abing.spider.task.pageprocessor.amac;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.amac.FundAccountEntity;
import tech.abing.spider.task.model.amac.FundAccountStatEntity;
import tech.abing.spider.task.model.amac.FundStatEntity;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

public class FundAccountProcessor implements PageProcessor{

	private static Logger logger = Logger.getLogger(FundAccountProcessor.class);
	
	private static final String baseUrl = "http://gs.amac.org.cn/amac-infodisc/res/fund/account/";
	
	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		String json = page.getContent();
		logger.info(json);
		JSONObject jsonObj = JSONObject.fromObject(json);
		
		boolean first = jsonObj.getBoolean("first");
		if(first){
			//总页数
			long totalPages = jsonObj.getLong("totalPages");
			
			long totalCount = jsonObj.getLong("totalElements");
			FundAccountStatEntity stat = new FundAccountStatEntity();
			stat.setId(UuIdPublic.timeStrongID(5));
			stat.setFundAccountCount(String.valueOf(totalCount));
			stat.setCreateTime(new Date());
			objectContainer.add(stat);
			
			PageRequest request = page.getRequest();
			for(int i = 1; i < totalPages; i++){
				
				String url = request.getUrl().replace("&page=0&", "&page="+i+"&");
				PageRequest req = context.createPageRequest(url, FundAccountProcessor.class);
				
				req.setMethod(request.getMethod());
				req.setPostType(request.getPostType());
				req.setReqPayloadParams(request.getReqPayloadParams());
				
				req.setHeaders(request.getHeaders());
				
				queue.add(req);
				
			}
		}
		
		//解析数据
		JSONArray content = jsonObj.getJSONArray("content");
		
		if(content !=null && content.size()>0){
			for(int i = 0;i<content.size();i++){
				JSONObject ctxObj = content.getJSONObject(i);
				FundAccountEntity fa = new FundAccountEntity();
				fa.setId(UuIdPublic.timeStrongID(5));
				if(!"null".equals(ctxObj.getString("bailor"))){
					fa.setBailor(ctxObj.getString("bailor"));
				}
				if(!"null".equals(ctxObj.getString("contractDeadLine"))){
					fa.setContractDeadline(ctxObj.getString("contractDeadLine"));
				}
				if(!"null".equals(ctxObj.getString("hasClassify"))){
					fa.setHasClassify(ctxObj.getString("hasClassify"));
				}
				fa.setId(ctxObj.getString("id"));
				if(!"null".equals(ctxObj.getString("investNumOfCreated"))){
					fa.setInvestNumofCreated(ctxObj.getInt("investNumOfCreated"));
				}
				if(!"null".equals(ctxObj.getString("investScope"))){
					fa.setInvestScope(ctxObj.getString("investScope"));
				}
				fa.setManager(ctxObj.getString("manager"));
				fa.setName(ctxObj.getString("name"));
				if(!"null".equals(ctxObj.getString("productType"))){
					fa.setProductType(ctxObj.getString("productType"));
				}
				fa.setRegisterCode(ctxObj.getString("registerCode"));
				if(!"null".equals(ctxObj.getString("registerDate"))){
					fa.setRegisterDate(new Date(ctxObj.getLong("registerDate")));
				}
				if(!"null".equals(ctxObj.getString("scaleOfCreated"))){
					fa.setScaleOfCreated(ctxObj.getDouble("scaleOfCreated"));
				}
				fa.setAccountType(ctxObj.getString("type"));
				if("一对一".equals(ctxObj.getString("type"))){
					fa.setCreateTime(new Timestamp(System.currentTimeMillis()));
					StringUtils.removeSpace(fa);
					objectContainer.add(fa);
				}else{
					String url = baseUrl+fa.getId()+".html";
					PageRequest req = context.createPageRequest(url, FundAccountDetailProcessor.class);
					req.getExtParams().put("fa", fa);
					queue.add(req);
				}
				
			}
		}
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}


}
